package com.oracle.oa.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.ManualSignBiz;
import com.oracle.oa.entity.Manualsign;

public class DoMenu extends ActionSupport implements SessionAware {

	public List<Manualsign> ondaymslist;
	Map<String, Object> session;
	
	public ManualSignBiz msbiz;
	
	public String type;

	@Override
	public String execute() throws Exception {
		
		String tiao = "";
		
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		
		
		//根据id查询所有的签到信息
		
		List<Manualsign> mslist=msbiz.getByUserid(Integer.parseInt(session.get("userid").toString()));
		//查询出前一天所有的签到信息。放在lastdaylist中
		List<Manualsign> lastdayslist=new ArrayList<Manualsign>();
		for(Manualsign ondayms:mslist){
			try {
				if((sf.format(sf.parse(ondayms.getSigntime()))).equals(session.get("last"))){
					lastdayslist.add(ondayms);
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//查询出当天所有的签到信息，放在ondaymslist中
		List<Manualsign> ondaymslist=new ArrayList<Manualsign>();
		for(Manualsign ondayms:mslist){
			try {
				if((sf.format(sf.parse(ondayms.getSigntime()))).equals(session.get("day"))){
					ondaymslist.add(ondayms);
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//判断是否周六周日，如果是周六周日，不需要签到
		if(java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_WEEK)==7 || java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_WEEK)==1 && type==null){
			tiao="signno";
		}else{
		
		//查询昨天签到条数
		if(lastdayslist!=null && lastdayslist.size()>0){
			List<Manualsign> lastlist = lastdayslist;
			if(lastlist.size()==0){
				tiao = "signin";
			}else if(lastlist.size()==1){
				tiao="signbu";
			}else if(lastlist.size()==2){
				// 查询当日签到条数
				
				List tag = new ArrayList();

				if (ondaymslist != null && ondaymslist.size()>0) {
					List<Manualsign> list = ondaymslist;
					for (Manualsign ms : list) {
						tag.add(ms.getSigntag());
					}
					if (tag.size() == 0) {
						// response.sendRedirect("signin.jsp");
						tiao = "signin";
					} else if (tag.size() == 1) {
						// response.sendRedirect("signoff.jsp");
						tiao = "signoff";
					}else if(tag.size()==2){
						tiao="signscs";
					}
				}else{
					tiao="signin";
				}
			}
		}else{
			// 查询当日签到条数
			
			List tag = new ArrayList();

			if (ondaymslist != null && ondaymslist.size()>0) {
				List<Manualsign> list = ondaymslist;
				for (Manualsign ms : list) {
					tag.add(ms.getSigntag());
				}
				if (tag.size() == 0) {
					// response.sendRedirect("signin.jsp");
					tiao = "signin";
				} else if (tag.size() == 1) {
					// response.sendRedirect("signoff.jsp");
					tiao = "signoff";
				}else if(tag.size()==2){
					tiao="signscs";
				}
			}else{
				tiao="signin";
			}
		}
		}
		return tiao;
	}

	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public ManualSignBiz getMsbiz() {
		return msbiz;
	}

	public void setMsbiz(ManualSignBiz msbiz) {
		this.msbiz = msbiz;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
